/*
 * FileUploadAgent.java
 *
 * Created on 11 październik 2007, 21:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.mikrotubula.agents;

import info.mikrotubula.managers.ImagesManagerLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.myfaces.custom.fileupload.UploadedFile;

/**
 *
 * @author Krystian
 */
public class FileUploadAgent {
    private UploadedFile _upFile;
    private String _name = "";
    private SessionsAgent sessionsAgent;
    @EJB
    private ImagesManagerLocal imagesManagerBean;
    
    
    public UploadedFile getUpFile()
    {
        return _upFile;
    }

    public void setUpFile(UploadedFile upFile)
    {
        _upFile = upFile;
    }

    public String getName()
    {
        return _name;
    }

    public void setName(String name)
    {
        _name = name;
    }

    public String upload() throws IOException
    {
        System.out.print("nana2");
        if (!_upFile.getContentType().startsWith("image/"))
            FacesContext.getCurrentInstance().addMessage("login-box", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Obrazy: wybrany plik nie jest obrazem", "Wybrany plik nie jest obrazem"));
       
        imagesManagerBean.create(new Integer(0) ,getName(), sessionsAgent.getSession().getUser());
        return "ok";
    }

    public boolean isUploaded()
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getExternalContext().getRequestMap().get("fileupload_bytes")!=null;
    }

    public SessionsAgent getSessionsAgent() {
        return sessionsAgent;
    }

    public void setSessionsAgent(SessionsAgent sessionsAgent) {
        this.sessionsAgent = sessionsAgent;
    }
}
